#include <iostream>
#include <cstring>
#include <cmath>
#include <stack>
#include <iomanip>
#include <cstdlib>
#include <complex>
#include <complex.h>

using namespace std;

stack<char> opt;
stack<double> val;

const int IN = 0;
const int OUT = 1;

char opt_set[10] = "+-*/=.()";

int level(char theOpt)
{
	switch (theOpt) {
	case '(':
		return 0;
	case '+':
	case '-':
		return 1;
	case '*':
	case '/':
		return 2;
	case ')':
		return 3;
	default:
		return -1;
	}
}

bool in_set(char theChar) {
	for (int i = 0; i < 8; i++) {
		if (theChar == opt_set[i])
			return true;
	}
	return false;
}

bool in_sets(char theChar) {
	for (int i = 0; i < 6; i++) {
		if (theChar == opt_set[i])
			return true;
	}
	return false;
}

bool del_space(string& theString)
{
	string res;
	for (int i = 0; i < theString.length(); i++) {
		if (in_set(theString[i]) || isdigit(theString[i])){
			res += theString[i];
		}
		else if (theString[i] == ' ') {}
		else{
			return false;
		}
	}
	theString = res;
	return true;
}

bool check(string& res)
{
	int balance = 0;
	string ress;
	if (in_sets(res.front()) || in_sets(res.back())) return false;
	for (int i = 0; i < res.length(); i++){
		if (res[i] == '(') balance ++;
		if (res[i] == ')') balance --;
		if (in_sets(res[i]) && i > 0 && in_sets(res[i-1])) return false;
		if (in_sets(res[i]) || isdigit(res[i])){
			ress += res[i];
		}
	}
	if (balance != 0) return false;
	if (ress.length() == 0) return false;
	for (int i = 0; i < ress.length(); i++){
		if (in_sets(ress[i]) && i > 0 && in_sets(ress[i-1])) return false;
	}
	return true;
}

string to_string(int theInt)
{
	if (theInt == 0)
		return string("0");
	bool neg = false;
	if (theInt < 0)
	{
		neg = true;
		theInt = -theInt;
	}
	string res;
	while (theInt != 0)
	{
		char c = (theInt % 10) + '0';
		res = c + res;
		theInt /= 10;
	}
	if (neg)
		res = '-' + res;
	return res;
}

bool change(string &from, string &to)
{
	int theInt = 0;
	int state = OUT;
	char c;

	for (int i = 0; i < from.length(); i++)
	{
		c = from[i];
		if (isdigit(c)) {
			to = to + c;
			theInt *= 10;
			theInt += c - '0';
			state = IN;
		}
		else{
			if (state == IN && c == '.') {
				to = to + '.';
				theInt = 0;
				continue;
			}
			if (state == IN && c != '.') {
				to +=  ' ';
				theInt = 0;
			}
			if (c == '=')
				break;
			else if (c == '(')
				opt.push(c);
			else if (c == ')') {
				while (!opt.empty() && opt.top() != '(') {
					to += opt.top();
					to += ' ';
					opt.pop();
				}
				opt.pop();
			}
			else {
				while (true) {
					if (opt.empty() || opt.top() == '(')
						opt.push(c);
					else if (level(c) > level(opt.top()))
						opt.push(c);
					else {
						to += opt.top();
						to += ' ';
						opt.pop();
						continue;
					}
					break;
				}
			}
			state = OUT;
		}
	}
	while (!opt.empty()) {
		to += opt.top();
		to += ' ';
		opt.pop();
	}
	return true;
}

bool compute(string &theExp)
{
	int theInt = 0;
	int state = OUT;
	char c;
	bool dot = false;
	double count = 1.0;
	for (int i = 0; i < theExp.length(); i++)
	{
		c = theExp[i];
		if (isdigit(c)|| c == '.') {
			if (isdigit(c)) {
				theInt *= 10;
				theInt += c - '0';
				state = IN;
				if (dot == true)
					count *= 10.0;
			}
			if (c == '.') {
				dot = true;
				continue;
			}
		}
		else{
			dot = false;
			double ans = theInt / count;			
			count = 1.0;
			if (state == IN) {
				val.push(ans);
				theInt = 0;
			}
			double x, y;
			if (c != ' ') {
				x = val.top();val.pop();
				y = val.top();val.pop();

				switch (c) {
				case '+':val.push(x + y); break;
				case '-':val.push(y - x); break;
				case '*':val.push(x * y); break;
				case '/':
				if(x != 0){
				val.push(y / x);break;}
				else return false;
				default:cout << "ILLEGAL" << endl;
				}
			}
			state = OUT;
		}
	}
	return true;
}

